<template>
	<el-container :class="['homecontainer']">
		<!-- header头部区域 -->
		<el-header>
			<div>
				<img src="../assets/logo.png" />
				<span>电商后台管理系统</span>
			</div>
			<el-button type="info" @click="logout">退出登录</el-button>
		</el-header>
		<!-- 内容区 -->
		<el-container class="routercontainer">
			<!-- 左边侧边栏区域 -->
			<el-aside class="routeraside" :width="isCollapse?'64px':'200px'">
				<!-- 折叠按钮 -->
				<div class="toggle_button" @click="toggleCollapse" style="user-select:none;">|||</div>
				<!-- 左侧菜单栏   router default-active 属性配合使用实现点击菜单栏路由跳转功能-->
				<el-menu
					background-color="#333744"
					text-color="#fff"
					active-text-color="#409eff"
					unique-opened
					:collapse="isCollapse"
					:collapse-transition="false"
					:router="true"
					:default-active="'/' + activeUrl"
				>
					<!-- 一级菜单 -->
					<el-submenu :index="item.id +''" v-for="item in menuList" :key="item.id">
						<template slot="title">
							<i :class="iconsList[item.id]"></i>
							<span>{{item.authName}}</span>
						</template>
						<!-- 二级菜单 -->
						<el-menu-item
							:index="'/'+subItem.path"
							v-for="subItem in item.children"
							:key="subItem.id"
							@click="activeUrls(subItem.path)"
						>
							<template slot="title">
								<i class="el-icon-menu"></i>
								<span>{{subItem.authName}}</span>
							</template>
						</el-menu-item>
					</el-submenu>
				</el-menu>
			</el-aside>
			<!-- 右边主体区域 -->
			<el-main :class="isCollapse?'routermain2':'routermain1'">
				<!-- 子路由占位符 -->
				<router-view></router-view>
			</el-main>
		</el-container>
	</el-container>
</template>

<script>
export default {
	data() {
		return {
			// 左侧菜单数据
			menuList: [],
			iconsList: {
				'125': 'el-icon-s-custom',
				'103': 'el-icon-s-unfold',
				'101': 'el-icon-s-goods',
				'102': 'el-icon-s-order',
				'145': 'el-icon-s-marketing'
			},
			//是否折叠
			isCollapse: false,
			activeUrl: ''
		}
	},
	created() {
		this.getMenuList()
		this.activeUrl = window.sessionStorage.getItem('activeUrls')
	},
	methods: {
		logout() {
			window.sessionStorage.clear()
			this.$router.push('/login')
		},
		async getMenuList() {
			//获取所有菜单
			const { data: res } = await this.$http.get('menus')
			if (res.meta.status !== 200)
				return this.$message.error({
					message: res.meta.msg,
					duration: 1000,
					offset: 100
				})
			this.menuList = res.data
		},
		toggleCollapse() {
			//菜单折叠与展开
			this.isCollapse = !this.isCollapse
		},
		activeUrls(url) {
			//二级菜单高亮
			this.activeUrl = url
			window.sessionStorage.setItem('activeUrls', url)
		}
	}
}
</script>

<style lang="less" scoped>
.homecontainer {
	height: 100%;
}

.el-header {
	background-color: #373d41;
	display: flex;
	justify-content: space-between;
	padding-left: 5px;
	align-items: center;
	color: #fff;
	font-size: 20px;
	div {
		height: 100%;
		img {
			height: 100%;
			vertical-align: middle;
			border-radius: 50%;
			margin-right: 10px;
		}
	}
}
.el-aside {
	background-color: #333744;
	.toggle_button {
		background-color: #4a5064;
		font-size: 10px;
		line-height: 24px;
		color: #fff;
		text-align: center;
		letter-spacing: 0.2em;
		cursor: pointer;
	}
	.el-menu {
		border-right: 0px;
	}
}
.el-main {
	background-color: #eaedf1;
}
</style>